package samfeng.erp.location.dal.mysql;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import org.apache.ibatis.annotations.Mapper;
import samfeng.erp.location.controller.vo.*;
import samfeng.erp.location.dal.dataobject.LocationDO;

/**
 * 仓库库位 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface LocationMapper extends BaseMapperX<LocationDO> {

    default PageResult<LocationDO> selectPage(LocationPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<LocationDO>()
                .likeIfPresent(LocationDO::getName, reqVO.getName())
                .likeIfPresent(LocationDO::getCompleteName, reqVO.getCompleteName())
                .eqIfPresent(LocationDO::getUsageType, reqVO.getUsageType())
                .eqIfPresent(LocationDO::getWarehouseId, reqVO.getWarehouseId())
                .eqIfPresent(LocationDO::getScrapLocation, reqVO.getScrapLocation())
                .eqIfPresent(LocationDO::getReturnLocation, reqVO.getReturnLocation())
                .eqIfPresent(LocationDO::getSequence, reqVO.getSequence())
                .orderByDesc(LocationDO::getId));
    }

    default List<LocationDO> selectList(LocationExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<LocationDO>()
                .betweenIfPresent(LocationDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(LocationDO::getStatus, reqVO.getStatus())
                .likeIfPresent(LocationDO::getName, reqVO.getName())
                .likeIfPresent(LocationDO::getCompleteName, reqVO.getCompleteName())
                .eqIfPresent(LocationDO::getUsageType, reqVO.getUsageType())
                .eqIfPresent(LocationDO::getPosx, reqVO.getPosx())
                .eqIfPresent(LocationDO::getPosy, reqVO.getPosy())
                .eqIfPresent(LocationDO::getPosz, reqVO.getPosz())
                .eqIfPresent(LocationDO::getScrapLocation, reqVO.getScrapLocation())
                .eqIfPresent(LocationDO::getReturnLocation, reqVO.getReturnLocation())
                .eqIfPresent(LocationDO::getCyclicInventoryFrequency, reqVO.getCyclicInventoryFrequency())
                .betweenIfPresent(LocationDO::getLastInventoryDate, reqVO.getLastInventoryDate())
                .betweenIfPresent(LocationDO::getNextInventoryDate, reqVO.getNextInventoryDate())
                .eqIfPresent(LocationDO::getParentId, reqVO.getParentId())
                .eqIfPresent(LocationDO::getRemovalStrategyId, reqVO.getRemovalStrategyId())
                .eqIfPresent(LocationDO::getWarehouseId, reqVO.getWarehouseId())
                .eqIfPresent(LocationDO::getStorageCategoryId, reqVO.getStorageCategoryId())
                .eqIfPresent(LocationDO::getDescription, reqVO.getDescription())
                .eqIfPresent(LocationDO::getSequence, reqVO.getSequence())
                .orderByDesc(LocationDO::getId));
    }

    /**
     * 查询库位列表（排除节点）
     */
    default List<LocationDO> excludeSelectList(Long id, Long warehouseId) {
        List<LocationDO> locationDOS;
        if (id == 0) {
            locationDOS = selectList(new LambdaQueryWrapperX<LocationDO>()
                    .eqIfPresent(LocationDO::getWarehouseId, warehouseId)
                    .orderByAsc(LocationDO::getSequence));
        } else {
            locationDOS = selectList(new LambdaQueryWrapperX<LocationDO>()
                    .notIn(LocationDO::getId, id)
                    .eq(LocationDO::getWarehouseId, warehouseId)
                    .orderByAsc(LocationDO::getSequence));
        }
        return locationDOS;
    }

}
